package com.lxg.pojo;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.*;

/**
 * @author 陆小根
 * date: 2022/04/23
 * Description:
 * 自定义用户对象
 */

public class User implements UserDetails {

  private Integer id;
  private String username;
  private String password;
  private Boolean enabled; // 账号是否激活
  private Boolean accountNonExpired; // 账户是否过期
  private Boolean accountNonLocked; // 账号是否锁定
  private Boolean credentialsNonExpired; // 密码是否过期
  private List<Role> roles = new ArrayList<>(); // 关系属性，用来存储当前用户所有角色信息


  // 返回权限信息
  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    Set<SimpleGrantedAuthority> authorities = new HashSet<>();
    roles.forEach(role -> {
      SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(role.getName());
      authorities.add(simpleGrantedAuthority);
    });
    return authorities;
  }

  @Override
  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  @Override
  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  @Override
  public boolean isAccountNonExpired() {
    return accountNonExpired;
  }

  public void setAccountNonExpired(Boolean accountNonExpired) {
    this.accountNonExpired = accountNonExpired;
  }

  @Override
  public boolean isAccountNonLocked() {
    return accountNonLocked;
  }

  public void setAccountNonLocked(Boolean accountNonLocked) {
    this.accountNonLocked = accountNonLocked;
  }

  @Override
  public boolean isCredentialsNonExpired() {
    return credentialsNonExpired;
  }

  public void setCredentialsNonExpired(Boolean credentialsNonExpired) {
    this.credentialsNonExpired = credentialsNonExpired;
  }

  @Override
  public boolean isEnabled() {
    return enabled;
  }

  public void setEnabled(Boolean enabled) {
    this.enabled = enabled;
  }

  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public List<Role> getRoles() {
    return roles;
  }

  public void setRoles(List<Role> roles) {
    this.roles = roles;
  }
}
